package com.vm.action.system;

import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Preparable;
import com.vm.action.ActionSuper;
import com.vm.common.Page;
import com.vm.dao.system.ICodeClass;
import com.vm.entity.CodeClass;
import com.vm.entity.User;
import com.vm.log4j.CustomizeLog4j;

@SuppressWarnings("serial")
public class CodeClassAction extends ActionSuper implements Preparable{
	private static Logger _log = CustomizeLog4j.getLogger(CodeClassAction.class);
	private CodeClass codeClass;
	private ICodeClass codeClassDAO;
	private List<CodeClass> listCodeClass;
	private Page<CodeClass> page;
	private String pageLinkList ;
	
	private String sarchClassCd;
	private String sarchClassName;
	
	
	public void prepare() throws Exception {
		super.setButtonGrpMap(codeClassDAO.seletButtonGrp((User)this.getSession().get("user"), "codeClassAction"));
	}

	public String list() throws Exception {
		User user =(User)this.getSession().get("user");
		String hql = "from CodeClass where userEditFlag='1' ";
		if(user.isSuperAdmin()){
			   hql = "from CodeClass where (1=1) ";	
		}
		String url = "/maintain/codeClassAction_list.do?";
		List<String> wherList = new ArrayList<String>();
		if(sarchClassCd != null && !"".equals(sarchClassCd)){
		   hql += " and classCd like ?";	
		   url +="&sarchClassCd="+sarchClassCd;
		   wherList.add("%"+ sarchClassCd+"%");
		}
		if(sarchClassName != null && !"".equals(sarchClassName)){
		   hql += " and className like ?";
		   url +="&sarchClassName="+sarchClassName;
		   wherList.add("%"+sarchClassName+"%");
		}
		page = this.codeClassDAO.page(hql, "select count(*)"+hql, wherList.toArray(), PAGE_ROW_SIZE, super.getPageNumber());
		this.pageLinkList = super.getSplitPageLink(url, page);
		if(user.getGrpUrlMap() != null){
			user.getGrpUrlMap().put("codeAction", user.getGrpUrlMap().get("codeClassAction"));
		}
		return SUCCESS;
	}

	public String add() throws Exception {
		return SUCCESS;
	}

	public String save() throws Exception {
		String[] msgArg = new String[] { "codeClassAction_list.do","codeClassAction_add.do" };
		String msg = null;
		try {
			
			if(codeClassDAO.findByClassCd(codeClass.getClassCd()).size() > 0){
				this.addFieldError("msg", this.getText("insert_name"));
				return INPUT;
			}else{
				codeClass.setUpdateTime(new Timestamp(System.currentTimeMillis()));
				codeClassDAO.save(codeClass);
				msg = super.getResourceMessage("insert_codeClass_success", msgArg);
			}
		} catch (Exception e) {
			_log.error("", e);
			msg = super.getResourceMessage("insert_codeClass_err", msgArg);
		}

		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;

	}

	public String edit() throws Exception {
		codeClass = codeClassDAO.findById(codeClass.getClassCd());
		return SUCCESS;
	}

	public String update() throws Exception {
		String[] msgArg = new String[] { "codeClassAction_list.do","codeClassAction_edit.do?codeClass.classCd="+codeClass.getClassCd()};
		String msg = null;
		try {
			codeClass.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			codeClassDAO.update(codeClass);
			msg = super.getResourceMessage("update_codeClass_success", msgArg);
		} catch (Exception e) {
			_log.error("", e);
			msg = super.getResourceMessage("update_codeClass_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}

	public String delete() throws Exception {
		String[] msgArg = new String[] { "codeClassAction_list.do" };
		String msg = null;
		try {
			codeClassDAO.deleteCode(codeClass.getClassCd());
			codeClassDAO.deleteById(codeClass.getClassCd());
			msg = super.getResourceMessage("delete_codeClass_success", msgArg);
		} catch (Exception e) {
			_log.error("", e);
			msg = super.getResourceMessage("delete_codeClass_err", msgArg);
		}

		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}

	public CodeClass getCodeClass() {
		return codeClass;
	}

	public void setCodeClass(CodeClass codeClass) {
		this.codeClass = codeClass;
	}


	public ICodeClass getCodeClassDAO() {
		return codeClassDAO;
	}

	public void setCodeClassDAO(ICodeClass codeClassDAO) {
		this.codeClassDAO = codeClassDAO;
	}

	public List<CodeClass> getListCodeClass() {
		return listCodeClass;
	}

	public void setListCodeClass(List<CodeClass> listCodeClass) {
		this.listCodeClass = listCodeClass;
	}

	public Page<CodeClass> getPage() {
		return page;
	}

	public void setPage(Page<CodeClass> page) {
		this.page = page;
	}

	public String getPageLinkList() {
		return pageLinkList;
	}

	public void setPageLinkList(String pageLinkList) {
		this.pageLinkList = pageLinkList;
	}

	public String getSarchClassCd() {
		return sarchClassCd;
	}

	public void setSarchClassCd(String sarchClassCd) {
		this.sarchClassCd = sarchClassCd;
	}

	public String getSarchClassName() {
		return sarchClassName;
	}

	public void setSarchClassName(String sarchClassName) {
		this.sarchClassName = sarchClassName;
	}
	
}
